package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogUnionHuaBenGuanPu;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.unionhuaben.HuaBenGuanPuResult;
import com.yanqu.road.server.manger.activity.unionhuaben.UnionHuaBenActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_CROSS_UNION_HUA_BEN_GUAN_PU, desc = "关扑")
public class UnionHuaBenGuanPuCmd extends AbstractCommand {

    UnionHuaBenProto.UnionHuaBenGuanPuRespMsg.Builder respMsg = UnionHuaBenProto.UnionHuaBenGuanPuRespMsg.newBuilder();

    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionHuaBenProto.UnionHuaBenGuanPuReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenGuanPuReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        respMsg.setType(type);
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        int times = type > 0 ? 10 : 1;

        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        Property consume = PropertyHelper.parseStringToProperty(GameConfig.UNION_STORY_SCORE_ID + "=" + GameConfig.UNION_STORY_DRAW_COST);
        if(times > 1){
            consume.rideProperty(times);
        }
        if(!currencyModule.removeCurrency(consume, eLogMoneyType.CrossUnionHuaBen, eLogMoneyType.CrossUnionHuaBenGuanPuConsume)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        //活动配置
        if(UnionHuaBenActivityMgr.getActivityInfo() != null){
            UnionHuaBenProto.UnionHuaBenGuanPuHistoryRecordUploadMsg.Builder uploadMsg = UnionHuaBenProto.UnionHuaBenGuanPuHistoryRecordUploadMsg.newBuilder();
            Property reward = new Property();
            for (int i = 0; i < times; i++) {
                HuaBenGuanPuResult rewardResult = UnionHuaBenActivityMgr.randomReward();
                if(!StringUtils.isNullOrEmpty(rewardResult.getReward())){
                    currencyModule.addCurrency(rewardResult.getReward(), eLogMoneyType.CrossUnionHuaBen, eLogMoneyType.CrossUnionHuaBenGuanPuReward);
                    reward.addProperty(PropertyHelper.parseStringToProperty(rewardResult.getReward()));

                    if(UnionHuaBenActivityMgr.isRewardIdxNeedNotice(rewardResult.getIdx())){
                        UnionHuaBenProto.UnionHuaBenGuanPuHistoryRecordTemp.Builder recordTempBuilder = UnionHuaBenProto.UnionHuaBenGuanPuHistoryRecordTemp.newBuilder();
                        recordTempBuilder.setNickName(player.getUserInfo().getNickName());
                        recordTempBuilder.setRewards(rewardResult.getReward());
                        recordTempBuilder.setServerId(GameServer.getInstance().getServerId());
                        recordTempBuilder.setUserId(player.getUserId());
                        recordTempBuilder.setTime(System.currentTimeMillis());
                        uploadMsg.addRecord(recordTempBuilder);
                    }
                }
            }
            respMsg.setReward(PropertyHelper.parsePropertyToString(reward));

            //上传
            if(uploadMsg.getRecordCount() > 0){
                player.sendPacket(Protocol.C_CROSS_UNION_HUA_BEN_GUAN_PU_RECORD_UPLOAD, uploadMsg);
            }
        }else {
            if(GoodsMgr.isGoodsBag(GameConfig.UNION_STORY_DRAW_REWARD)){
                Property reward = new Property();
                for (int i = 0; i < times; i++) {
                    OpenGoodsBagResult openResult = GoodsBagMgr.getRandomGoods(GameConfig.UNION_STORY_DRAW_REWARD);
                    currencyModule.addCurrency(openResult.getReward(), eLogMoneyType.CrossUnionHuaBen, eLogMoneyType.CrossUnionHuaBenGuanPuReward);
                    reward.addProperty(openResult.getReward());
                }
                respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
            }else {
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(GameConfig.UNION_STORY_DRAW_REWARD);
                if(goodsInfo == null){
                    ret = GameErrorCode.E_GOODS_NO_EXIST;
                    return;
                }
                currencyModule.addCurrency(GameConfig.UNION_STORY_DRAW_REWARD + "=" + times, eLogMoneyType.CrossUnionHuaBen, eLogMoneyType.CrossUnionHuaBenGuanPuReward);
                respMsg.setReward(GameConfig.UNION_STORY_DRAW_REWARD + "=" + times);
            }
        }


        //日志
        AutoLogMgr.add(new LogUnionHuaBenGuanPu(player.getUserInfo().getUnionUid(), player.getUserId(), times, respMsg.getReward()));

        respMsg.setRet(0);
        player.sendPacket(Protocol.U_CROSS_UNION_HUA_BEN_GUAN_PU, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_CROSS_UNION_HUA_BEN_GUAN_PU, respMsg.setRet(ret).setType(type));
        }
    }
}
